//
//  AreaInfoModel.m
//  XV
//
//  Created by apple on 31/05/2019.
//  Copyright © 2019 XR. All rights reserved.
//

#import "AreaInfoModel.h"
#import "MJExtension.h"


@implementation AreaInfoModel

+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
    return @{
             @"ID":@"id"
             };
}

+ (NSDictionary *)mj_objectClassInArray
{
    return @{
             @"son" : @"AreaInfoModel"
             };
}

@end


@implementation AreaInfoManage

//自定义初始化
//重写init方法
-(instancetype)init
{
    if (self = [super init])
    {
        [self loadLocalData];
    }
    return self;
}

//先加载本地数据
-(void)loadLocalData
{
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"AreaInfo" ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    
    // 对数据进行JSON格式化并返回字典形式
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    
    self.areaList = [AreaInfoModel mj_objectArrayWithKeyValuesArray:json];
}

//省名转ID
-(NSString*)provinceIDFrom:(NSString*)provinceName
{
    if (kStringIsEmpty(provinceName)) {
        return nil;
    }
    
    NSString* pid = nil;
    for (int i=0; i<self.areaList.count; i++)
    {
        AreaInfoModel* pmodel = [self.areaList objectAtIndex:i];
        
        if ([self s2s:pmodel.name str2:provinceName ignoreStr:@"省"])
        {
            pid = pmodel.ID;
            break;
        }
        //直辖市
        else if ([self s2s:pmodel.name str2:provinceName ignoreStr:@"市"])
        {
            pid = pmodel.ID;
            break;
        }
    }
    
    return pid;
}


//省ID和城市名得到城市ID
-(NSString*)cityIDFrom:(NSString*)provinceID cityName:(NSString*)cityName
{
    if (kStringIsEmpty(provinceID) || kStringIsEmpty(cityName)) {
        return nil;
    }
    NSMutableArray* clist = [self cityListFrom:provinceID];
    
    for (int i=0; i<clist.count; i++)
    {
        AreaInfoModel* cmodel = [clist objectAtIndex:i];
        
        if ([self s2s:cmodel.name str2:cityName ignoreStr:@"市"])
        {
            return cmodel.ID;
        }
    }
    
    return nil;
}


//省ID转城市列表
-(NSMutableArray*)cityListFrom:(NSString*)provinceID
{
    if (kStringIsEmpty(provinceID))
    {
        return nil;
    }
    
    for (int i=0; i<self.areaList.count; i++)
    {
        AreaInfoModel* pmodel = [self.areaList objectAtIndex:i];
        
        if ([pmodel.ID isEqualToString:provinceID])
        {
            return pmodel.son;
        }
    }
    
    return nil;
}


//判断2个字符串是否相等，忽略指定的字符串
-(BOOL)s2s:(NSString*)str1 str2:(NSString*)str2 ignoreStr:(NSString*)ignoreStr
{
    if (kStringIsEmpty(str1) || kStringIsEmpty(str2)) {
        return NO;
    }
    
    NSString* strn1 = str1;
    if (kStringIsEmpty(ignoreStr)==NO)
    {
        strn1 = [str1 stringByReplacingOccurrencesOfString:ignoreStr withString:@""];
    }
    
    NSString* strn2 = str2;
    if (kStringIsEmpty(ignoreStr)==NO)
    {
        strn2 = [str2 stringByReplacingOccurrencesOfString:ignoreStr withString:@""];
    }
    
    return [strn1 isEqualToString:strn2];
}



-(NSMutableArray*)areaList
{
    if (_areaList==nil)
    {
        _areaList = [[NSMutableArray alloc]init];
    }
    
    return _areaList;
}

@end
